package com.game.raceroom.player.effect
{
	import com.game.raceroom.Room;
	import com.game.raceroom.player.Player;
	import com.game.raceroom.player.contrl.ActorContrl;
	import com.game.raceroom.player.effect.skill.BlastEffect;
	import com.game.raceroom.player.effect.skill.HideEffect;
	import com.game.raceroom.player.effect.skill.JumpEffect;
	import com.game.raceroom.player.effect.skill.RepairEffect;
	import com.game.raceroom.player.skill.Skill;
	import com.net.NetTrans;
	import com.util.DEF;
	import com.util.HashMap;
	
	import flash.utils.ByteArray;

	public class ActionChecker
	{
		private var actorContrl:ActorContrl;
		private var actors:HashMap;
		private var room:Room;
		
		public function ActionChecker(room:Room,actorContrl:ActorContrl,actors:HashMap)
		{
			this.room=room;
			this.actorContrl=actorContrl;
			this.actors=actors;
			crashList=new CrashList(room);
			
		}
	    
		private var name:String;	
		private var action:int;


        public function checkActions(msg:ByteArray):void{
			   var ac:int=NetTrans.readByte(msg);

	    	 
	    			
	    		    if(ac==DEF.ACTION_CRASH){
						 doCrashAction(msg);
	    		    }else 
	    		    if(ac==DEF.ACTION_EFFECT){
	    		    	 doEffectAction(msg);
	    		    }else 
	    		    if(ac==DEF.ACTION_ADDITEM){
	    		    	 doAdditemAction(msg);
	    		    	
	    		    }
	    	 


		  
		    crashList.flush();
        }
//        var kk:int=0;
	public static var mul:Number = 0.001;
		public function doMoveAction(msg:ByteArray):void{
			var uid:int=NetTrans.readInt(msg);
			
			var actor:Player=(Player)(actors.getObjByKey(uid));
			   		if(actor==room.actorContrl)return;
			   		if(actor){
					  actor.onMoveAction(NetTrans.readInt(msg),
						  NetTrans.readShort(msg),NetTrans.readShort(msg),
						  NetTrans.readShort(msg), DEF.cDouble( NetTrans.readShort(msg)),
						  DEF.cDouble( NetTrans.readShort(msg)), DEF.cDouble( NetTrans.readShort(msg)),
						  NetTrans.readInt(msg),NetTrans.readInt(msg));
			        } 
    	 
		}
		public function doFixPosition(msg:ByteArray):void{
			        room.actorContrl.cacuErrAndFix(NetTrans.readShort(msg),NetTrans.readShort(msg));
		}
		
		public function doActionAction(msg:ByteArray):void{
			var uid:int=NetTrans.readInt(msg);
			
			var actor:Player=(Player)(actors.getObjByKey(uid));
		    	if(actor==room.actorContrl)return;
     		   		//trace("other move "+(kk++));
			   		if(actor){
					     actor.onAction(NetTrans.readInt(msg),NetTrans.readInt(msg));	
			        } 
		}
		private function doCrashAction(msg:ByteArray):void{
			var size:int=NetTrans.readByte(msg);
			for(var i:int=0;i<size;i++){
				var uid:int=NetTrans.readInt(msg);
	 
				var actor:Player=(Player)(actors.getObjByKey(uid));
	            if(actor){
					//actor._x=actions[++idx];
					//actor._y=actions[++idx];
					actor.cacuErrAndFix(NetTrans.readShort(msg),NetTrans.readShort(msg));
					
			        actor.sideClipContrl.addCrash(NetTrans.readDouble(msg),NetTrans.readDouble(msg));
				    var acode:int=NetTrans.readInt(msg);
			       // actorContrl.action.parseActionCode(acode);
			       actor.actionId=NetTrans.readInt(msg);
	            }
			}
			 
				 
		}
		private function doAdditemAction(msg:ByteArray):void{
			var itemid:int=NetTrans.readInt(msg);
			var eftId:int=NetTrans.readInt(msg);
			var x:Number=NetTrans.readShort(msg);
			var y:Number=NetTrans.readShort(msg);
			var addedItemEft:AddedItemEffect=new AddedItemEffect(this.room,itemid,x,y);
			addedItemEft.effectId=eftId;
			room.addEffect(addedItemEft);
		}
		private function doEffectAction(msg:ByteArray):void{
 	        var effectType:int=NetTrans.readByte(msg);
	    	switch(effectType){
	    		case Effect.EFFECT_TYPE_UNDER_ATK:
	    		    spellEffectAtk(msg,0);
	    		    break;
	    		case Effect.EFFECT_TYPE_UNDER_FATAL_ATK_L1:
	    		    spellEffectAtk(msg,1);
	    		    break;
	    		case Effect.EFFECT_TYPE_UNDER_FATAL_ATK_L2:
	    		    spellEffectAtk(msg,2);
	    		    break;
	    		case Effect.EFFECT_TYPE_UNDER_FATAL_ATK_L3:
	    			spellEffectAtk(msg,3);
	    			break;
	    		case Effect.EFFECT_TYPE_DODGE:
	    		    spellEffectDodge(msg);
	    		    break;
	    		case Effect.EFFECT_TYPE_RESIST:	
	    		    spellEffectResist(msg);
	    		    break;
	    		case Effect.EFFECT_TYPE_CAROTIC:	
	    		    spellEffectCarotic(msg);
	    		    break;
	    	    case Effect.EFFECT_TYPE_STRICK:	
	    		    spellEffectStrick(msg);
	    		    break;
	    	    case Effect.EFFECT_TYPE_JUMP:	
	    		    spellEffectJump(msg);
	    		    break;
	    	    case Effect.EFFECT_TYPE_HIDE:	
	    		    spellEffectHide(msg);
	    		    break;
	    	    case Effect.EFFECT_TYPE_FOCUS:	
	    		    spellEffectFocus(msg);
	    		    break;
	    	    case Effect.EFFECT_TYPE_REPAIR:	
	    		    spellEffectRepair(msg);
	    		    break;
	    	    case Effect.EFFECT_TYPE_REMOVE:	
	    		    spellEffectRemove(msg);
	    		    break;
	    	    case Effect.EFFECT_TYPE_BLAST:	
	    		    spellEffectBlast(msg);
	    		    break;
	    	}
	     
		}
		private function spellEffectDodge(msg:ByteArray):void{
		    var receiver:int=NetTrans.readInt(msg);
		    var initiator:int=NetTrans.readInt(msg);
		     var eft:DodgeEffect;

			 	  var actor:Player=(Player)(actors.getObjByKey(receiver));
   		 		  if(actor){
   		 			  eft=new DodgeEffect(actor);
   		 			  room.addEffect(eft);
   		 		  }
		}      
		private function spellEffectResist(msg:ByteArray):void{
		    var receiver:int=NetTrans.readInt(msg);
		    var initiator:int=NetTrans.readInt(msg);
		     var eft:ResistEffect;

			 	var actor:Player=(Player)(actors.getObjByKey(receiver));
   		 		if(actor){
   		 			eft=new ResistEffect(actor);
   		 			room.addEffect(eft);
   		 		}

		} 
		private function spellEffectCarotic(msg:ByteArray):void{
		    var receiver:int=NetTrans.readInt(msg);
		    var initiator:int=NetTrans.readInt(msg);
		    var eftId:int=NetTrans.readInt(msg);
		    var maxTime:int=NetTrans.readInt(msg);
		     var eft:CaroticEffect;
 
		 	var actor:Player=(Player)(actors.getObjByKey(receiver));
   	 		if(actor){
   	 			eft=new CaroticEffect(actor,eftId,maxTime);
   	 			room.addEffect(eft);
   	 		}
			 	 
			 
		} 
		private function spellEffectBlast(msg:ByteArray):void{
		    var receiver:int=NetTrans.readInt(msg);
		    var leftBlood:int=NetTrans.readInt(msg);
		    var decBlood:int=NetTrans.readInt(msg);
		    var eft:BlastEffect;
 
		 	var actor:Player=(Player)(actors.getObjByKey(receiver));
   	 		if(actor){
   	 			actor.resetAction();
   	 			actor.up_speed=0;
   	 			actor.down_speed=0;
   	 			eft=new BlastEffect(actor,room);
   	 			room.addEffect(eft);
      		    actor.refBlood(leftBlood);
   		 	   var  eft1:BloodEffect=new BloodEffect(actor,2,decBlood);
   		 	     room.addEffect(eft1);
   	 		}
			 	 
 
		}    
 
 
       private var crashList:CrashList;
       private  function spellEffectAtk(msg:ByteArray,level:int):void{
           
		    var receiver:int=NetTrans.readInt(msg);
		    var initiator:int=NetTrans.readInt(msg);
		    var leftBlood:int=NetTrans.readInt(msg);
		    var decBlood:int=NetTrans.readInt(msg);
		    var eft:BloodEffect;
           

             crashList.addCrashPlayer(initiator,receiver,level,decBlood);
 
			   var actor:Player=(Player)(actors.getObjByKey(receiver));
   		 		if(actor){
   		 			actor.refBlood(leftBlood);
   		 			//actor.actorDisp.addCrashFilter();
   		 			eft=new BloodEffect(actor,level,decBlood);
   		 		    room.addEffect(eft);
			 		  
				}	 
			 
			 
	 
        }
       private  function spellEffectRepair(msg:ByteArray):void{
           
		    var receiver:int=NetTrans.readInt(msg);
		    var initiator:int=NetTrans.readInt(msg);
		    var leftBlood:int=NetTrans.readInt(msg);
		    var incBlood:int=NetTrans.readInt(msg);
		    var eft:RepairEffect;
 
			    var actor:Player=(Player)(actors.getObjByKey(receiver));
   		 		if(actor){
   		 			actor.refBlood(leftBlood);
   		 			eft=new RepairEffect(actor,incBlood);
   		 		    room.addEffect(eft);
 
				}	 
		 	
        }
       private  function spellEffectRemove(msg:ByteArray):void{
           
		    var removeType:int=NetTrans.readInt(msg);
		    var removeId:int=NetTrans.readInt(msg);
 	        for(var i:int=0;i<room.effects.size();i++){
 	        	var eft:Effect=(Effect)(room.effects.getObjById(i));
 	        	if(removeType==0){
	        		if(eft.effectType!=Effect.EFFECT_TYPE_ADDEDITEM){
 	        			if(eft.effectId==removeId){
 	        			  eft.destroy();
 	        			  room.effects.remove(i);
 	        			  return 
 	        			}
 	  
 	        			 
 	        		}    		
 	        	}else{
 	        		if(eft.effectType==Effect.EFFECT_TYPE_ADDEDITEM){
 	        			if(eft.effectId==removeId){
 	        			   eft.destroy();
 	        			   room.effects.remove(i);
 	        			   return  
 	        			}
 	        		}
 	        	}
 	        }
		 	
        } 
         var lop:int=0;    
        private function  spellEffectJump(msg:ByteArray):void{
			 var receiver:int=NetTrans.readInt(msg);
			 var initiator:int=NetTrans.readInt(msg);
			 var a:Player=getPlayerByName(receiver);
	         if(!a)return ;
  
   		    var eft:JumpEffect=new JumpEffect(a);
   		    room.addEffect(eft);
			lop++;
			trace("jump "+lop);
        	;
        }
        private function  spellEffectHide(msg:ByteArray):void{
//			 trace("spell hide");
			 var receiver:int=NetTrans.readInt(msg);
			 var initiator:int=NetTrans.readInt(msg);
			 var a:Player=getPlayerByName(receiver);
	         if(!a)return ;
	         var isLocal:Boolean=false;
            if(a==actorContrl){
            	isLocal=true;
            }
   		    var eft:HideEffect=new HideEffect(a,isLocal);
   		    room.addEffect(eft);	
        }
        private function  spellEffectFocus(msg:ByteArray):void{
			 var receiver:int=NetTrans.readInt(msg);
			 var initiator:int=NetTrans.readInt(msg);
            if(receiver==actorContrl.userInfo.userID){
	            for(var i:int=1;i<=8;i++){
		    	    var skill:Skill=(Skill)(room.skillBar.skills.getObjByKey(i));
		    	        if(!skill)continue;
		    	       if(skill.skillType!=Skill.SKILL_TYPE_FOCUS)
		    	        skill.coldDown();
	        	
	        	}
            }
        }   
        public function spellSkillStrick(a:Player,x:Number,y:Number,ang:int):void{
      
            a.cacuErrAndFix(x,y);
		    if(a._angle!=ang){
      		    a._angle=ang;
      		    a.turnActor();
            }
			a.clearSideclip();
			a.up_speed=a.carInfo.maxUpSpeed;
			a.down_speed=0;
			a.resetRunParam();
	        if(a._up){
	        	if(a._relaseAccelerator){
	        		a.resetAction();
	        		a._up=true;
        		    a._relaseAccelerator=true;
	        	}else{
	        		a.resetAction();
        		    a._up=true;	
	        	}

        	}else{
        		a.resetAction();
        		a._up=true;
        		a._relaseAccelerator=true;
        	}
        	a.actionCode=a.action.getCurentActionCode();	
        	a.actorDisp.setStrick();	
        }    
        private function spellEffectStrick(msg:ByteArray):void{
//			 trace("spell strick");
			 var receiver:int=NetTrans.readInt(msg);
			 var initiator:int=NetTrans.readInt(msg);
			 var actionid:int=NetTrans.readInt(msg);
			 var x:Number=NetTrans.readShort(msg);
			 var y:Number=NetTrans.readShort(msg);
			 var ang:int=NetTrans.readInt(msg);
			 var a:Player=getPlayerByName(receiver);
	         if(!a)return  ;

            if(a==actorContrl){
            	return ;
            }

            spellSkillStrick(a,x,y,ang);
 
        }
//        private function actorSpelled(skillType:int):void{
//            for(var i:int=1;i<=room.skillBar.skills.size();i++){
//	    	    var skill:Skill=(Skill)(room.skillBar.skills.getObjByKey(i));
//	    	    if(skill.skillType==skillType){
//	    	    	skill.spell();
//	    	    	var str:int=actorContrl.now_str-skill.castStr;
//	    	    	actorContrl.refStr(str);
//	    	    	return;
//	    	    }
//        	
//        	}
//
//	    }
	    
		private function log(info:String):void{
			//room.log(info);
		}
        private function getPlayerByName(name:int):Player{

	        return (Player)(actors.getObjByKey(name));
				
        }
        
        
	}
}